﻿using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _CadastroCandidato : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnCadastro_Click(object sender, ImageClickEventArgs e)
    {
        if (!ClassesSiteIntranet.Validadores.ValidaCPF(txtCPF.Text))
        {
            Response.Write("<script>alert('CPF inválido'); history.go(-1);</script>");
        }
        else
        {
            CandidatoExiste(txtCPF.Text, txtDataNascimento.Text);
            if (!CandidatoExiste(txtCPF.Text, txtDataNascimento.Text))
                Response.Redirect("mensagens.aspx?id=3");
            else
            {
                SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
                SqlCommand insereEmpresa = null;
                try
                {
                    conexaoIntranet.Open();
                    string instrucao = "";
                    if (txtEmail.Text != "")
                        instrucao = "UPDATE CamaraInscritos SET Senha = '"+ txtSenha.Text.Replace("'","") +"', Email = '"+ txtEmail.Text +"', Telefone = '"+ txtFone.Text +"', Telefone2 = '"+ txtFone2.Text +"', Disponivel = 1 WHERE CPF = '"+ txtCPF.Text +"'";
                    else
                        instrucao = "UPDATE CamaraInscritos SET Senha = '" + txtSenha.Text.Replace("'", "") + "', Telefone = '"+ txtFone.Text +"', Telefone2 = '"+ txtFone2.Text +"', Disponivel = 1 WHERE CPF = '" + txtCPF.Text + "'";
                    insereEmpresa = new SqlCommand(instrucao, conexaoIntranet);
                    insereEmpresa.ExecuteNonQuery();
                    Response.Redirect("mensagens.aspx?id=2");
                }
                finally
                {
                    conexaoIntranet.Close();
                }
            }
        }
    }

    private bool CandidatoExiste(string cpf, string dataNascimento)
    {
        bool jaCadastrado = false;
        dataNascimento = Convert.ToDateTime(dataNascimento).ToString("MM-dd-yyyy");
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString.ToString());
        string instrucao = "SELECT CPF FROM CamaraInscritos WHERE CPF = '" + cpf + "' AND DataNascimento = '"+ dataNascimento +"'";
        try
        {
            if (conexaoIntranet.State != ConnectionState.Open)
                conexaoIntranet.Open();
            SqlCommand pesquisaInscrito = new SqlCommand(instrucao, conexaoIntranet);
            SqlDataReader drOficinas = pesquisaInscrito.ExecuteReader();
            jaCadastrado = drOficinas.HasRows;
            drOficinas.Close();
        }
        finally
        {
            conexaoIntranet.Close();
        }
        return jaCadastrado;
    }

}